package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    @RequestMapping("/loginByTelephone")
    public Result login(@RequestBody Map<String,String> map){
        log.debug("LoginController: login: map ===> " + map);
        String telephone = map.get("telephone");
        String validateCode = map.get("validateCode");
        String validateCodeRedis = jedisPool.getResource().get(RedisMessageConstant.SENDTYPE_LOGIN + "-" + telephone);
        if (validateCodeRedis==null || validateCodeRedis.length()==0){
            return new Result(false,"验证码失效");
        }
        if (!validateCodeRedis.equals(validateCode)){
            return new Result(false,"验证码错误");
        }
        memberService.add(telephone);
        log.debug("登录成功");
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

}
